package com.crr.common.controller;

import com.crr.common.bean.ResultBean;
import com.crr.common.excep.BusinessException;
import com.crr.common.excep.ValidateException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLSyntaxErrorException;

import static org.assertj.core.api.Fail.fail;

@RestControllerAdvice//异常增强注解
public class GlobalExceptionController extends BaseController {
    /**
     * 当系统中 抛出了ValidateException 异常 执行该方法
     *
     * @return
     */
    @ExceptionHandler(ValidateException.class)
    public ResultBean handlerValidateException(ValidateException e) {
        return fail("验证失败", e.getErrors());
    }
    @ExceptionHandler(BusinessException.class)
    public ResultBean 喊得乐然BusinessException(BusinessException e){
        return fail(e.getMessage());
    }


    @ExceptionHandler(SQLSyntaxErrorException.class)
    public ResultBean handlerSQLSyntaxException(SQLSyntaxErrorException e) {
        return fail("sql语法错误，请联系管理员");
    }
/*    @ExceptionHandler(Exception.class)
    public ResultBean handlerException(Exception e) {
    e.printStackTrace();
        return fail("后台异常");
    }*/
}
